<template>
  <van-form @submit="onSubmit">
    <van-field
      v-model="login.phone"
      name="phone"
      label="手机号"
      placeholder="手机号"
      :rules="[{ required: true, message: '请填写手机号' }]"
    />
    <van-field
      v-model="login.password"
      type="password"
      name="password"
      label="登录密码"
      placeholder="登录密码"
      :rules="[{ required: true, message: '请填写密码' }]"
    />
    <div style="margin: 16px">
      <van-button round block type="info" native-type="submit"
        >密码登录</van-button
      >
    </div>
  </van-form>
</template>

<script>
export default {
  name: "signin",
  data() {
    return {
      login: {
        phone: this.$route.query.phone,
      },
    };
  },
  methods: {
    onSubmit(val) {
      this.$ajax.login(val).then((res) => {
        if (res.code == 200) {
          localStorage.setItem("appphone", val.phone);
          localStorage.setItem("server_token", res.token);
          this.changeAppPhone(val.phone);
          this.resetUserInfo();
          this.$nextTick(() => {
            this.$router.push({ name: "mine" });
          });
        }
      });
    },
  },
  mounted() {
    this.login = {
      ...this.login,
      phone: this.$route.query.phone
        ? this.$route.query.phone
        : localStorage.getItem("appphone"),
    };
  },
};
</script>